﻿using System;
using AutoShuntingSchedule.BLL.Sys;
using AutoShuntingSchedule.UI.Common;
using AutoShuntingSchedule.Utility.Core;
using AutoShuntingSchedule.Utility.Security;
using Sunny.UI;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace AutoShuntingSchedule.UI.Forms
{
    public partial class FLogin : UILoginForm
    {
        private SysUserLogic userlogic;
        private SysUserLogOnLogic userLogOnLogic;

        public FLogin()
        {
            InitializeComponent();
            userlogic = new SysUserLogic();
            userLogOnLogic = new SysUserLogOnLogic();
            //UserName = "zxp";
            UserName = "admin";
            Password = "123456";
            //this.lblTitle.Text = $"中国铁道科学研究院";
            //this.lblSubText.Text = $"通信信号研究所";
        }

        private void FLogin_ButtonLoginClick(object sender, System.EventArgs e)
        {
            //UserName就是封装了界面里用户名输入框的值
            //Password就是封装了界面里密码输入框的值
            //if (UserName == "admin" && Password == "admin")
            //{
            //    IsLogin = true;
            //    Close();
            //}
            //else
            //{
            //    ShowErrorTip("用户名或者密码错误。");
            //}
            string userName = UserName;
            string password = Password;
            if (StringHelper.IsNullOrEmpty(userName))
            {
                this.ShowWarningDialog("请输入用户名", UIStyle.Inherited);
                return;
            }
            if (StringHelper.IsNullOrEmpty(password))
            {
                this.ShowWarningDialog("请输入密码", UIStyle.Inherited);
                return;
            }
            password = password.MD5Encrypt();
            var userEntity = userlogic.GetByUserName(userName);
            if (userEntity == null)
            {
                this.ShowInfoDialog("该账户不存在，请重新输入。", UIStyle.Inherited);
                return;
            }
            if (!userEntity.IsEnabled)
            {
                this.ShowInfoDialog("该账户已被禁用，请联系管理员。", UIStyle.Inherited);
                return;
            }
            var userLogOnEntity = userLogOnLogic.GetByAccount(userEntity.Id);
            string inputPassword = password.DESEncrypt(userLogOnEntity.SecretKey).MD5Encrypt();
            if (inputPassword != userLogOnEntity.Password)
            {
                this.ShowInfoDialog("密码错误，请重新输入。", UIStyle.Inherited);
                return;
            }
            userLogOnLogic.UpdateLogin(userLogOnEntity);
            GlobalConfig.CurrentUser = userEntity;
            if (!StringHelper.IsNullOrEmpty(userEntity.AvatarBase64))
            {
                try
                {
                    MemoryStream ms = new MemoryStream(Convert.FromBase64String(userEntity.AvatarBase64));
                    GlobalConfig.Avatar = Image.FromStream(ms);
                }
                catch
                {

                }
            }

            this.DialogResult = DialogResult.OK;
        }
    }
}